package com.newopenlab.hmc.util.stats;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.newopenlab.hmc.biz.Action;
import com.newopenlab.hmc.biz.Player;
import com.newopenlab.hmc.util.BaseHand;
import com.newopenlab.hmc.util.PlayerHandStat;
import com.newopenlab.hmc.util.Way;
import com.newopenlab.hmc.util.WayAction;

public class StatsUtil {

	public static boolean isPlayerBB(String playerName, BaseHand hand) {
		Boolean flag=false;
		flag=getBB(hand).name.equals(playerName);
		return flag;
	}

	public static BaseHand initStat(String playerName, BaseHand hand) {
		BaseHand bh=hand;
		PlayerHandStat phs=bh.stats.get(playerName);
		if(phs==null){
			phs=new PlayerHandStat();
			bh.stats.put(playerName, phs);
			phs.player=bh.getPlayerByName(playerName);
			
		}
		return bh;
	}
	
	public static Player getBB(BaseHand bh){
		Player p=null;
		for(WayAction wa:bh.actions){
			if(wa.action.equals(Action.BIG_BLIND.toString())){
				p=wa.player;
			}
		}
		return p;
	}
	
	public static Player getSB(BaseHand bh){
		Player p=null;
		for(WayAction wa:bh.actions){
			if(wa.action.equals(Action.SMALL_BLIND.toString())){
				p=wa.player;
			}
		}
		return p;
	}
	
	public static boolean isPlayerSB(String playerName, BaseHand bh) {
		Boolean flag=false;
		flag=getSB(bh).name.equals(playerName);
		return flag;
	}

	public static List<WayAction> filterBetsGTZ(String name,BaseHand bh, String[] actions,
			String[] ways) {
		Set<String> sa=new HashSet<String>(Arrays.asList(actions));
		Set<String> sw=new HashSet<String>(Arrays.asList(ways));
		List<WayAction> res=new ArrayList<WayAction>();
		for(WayAction wa:bh.actions){
			if((name == null || wa.player.name.equals(name)) && (sa.size()==0 ||  sa.contains(wa.action)) && (sw.size()==0 || sw.contains(wa.way)) && wa.chips>0){
				res.add(wa);
			}
		}
		return res;
	}
	
	
	
	
	
	public static Boolean lastAction(String name,BaseHand bh, String action,String	way) {
		Boolean flag=false;
		List<WayAction> res=filterBetsGTZ(null,bh,new String[]{action},new String []{way});
		String pname=null;
		
		for(WayAction wa:res){
			pname=wa.player.name;
		}
		flag=name.equals(pname);
		return flag;
	}
	
	public static Boolean firsrtAction(String name,BaseHand bh, String action,String	way) {
		Boolean flag=false;
		List<WayAction> res=filterBetsGTZ(null,bh,new String[]{action},new String []{way});
		String pname=null;
		for(WayAction wa:res){
			if(pname==null){
				pname=wa.player.name;
			}
		}
		flag=name.equals(pname);
		return flag;
	}
	
	public static Boolean is3Bet(String name,BaseHand bh, String way) {
		Boolean flag=false;
		List<WayAction> actions=null;
		actions=StatsUtil.filterBetsGTZ(null,bh,new String[]{Action.RAISE.toString()},new String[]{way});
		if(actions.size()>1){
			flag=actions.get(1).player.name.equals(name);
		}
		return flag;
	}
	
	public static Integer index(WayAction ac1,BaseHand bh){
		Integer pos=-1,ind=-1;
		Boolean flag=false;
		WayAction x=null;
		for(int i=0;i<bh.actions.size() && ! flag;i++){
			ind++;
			x=bh.actions.get(i);
			if(x.equals(ac1)){
				flag=true;
			}
		}
		if(flag){
			pos=ind;
		}
		return pos;
	}
	
	public static WayAction getBBAction(BaseHand bh){
		WayAction res=null;
		List<WayAction> actions=null;
		actions=StatsUtil.filterBetsGTZ(null,bh,new String[]{Action.BIG_BLIND.toString()},new String[]{Way.PREFLOP.toString()});
		if(actions.size()>0){
			res=actions.get(0);
		}
		return res;
	}
	
	public static WayAction getSBAction(BaseHand bh){
		WayAction res=null;
		List<WayAction> actions=null;
		actions=StatsUtil.filterBetsGTZ(null,bh,new String[]{Action.SMALL_BLIND.toString()},new String[]{Way.PREFLOP.toString()});
		if(actions.size()>0){
			res=actions.get(0);
		}
		return res;
	}
	
	public static Boolean after(WayAction ac1,WayAction ac2,BaseHand bh){
		Boolean flag=false;
		Integer pos1=index(ac1,bh);
		Integer pos2=index(ac2,bh);
		flag=pos1>pos2;
		return flag;
	}
	
	public static Boolean isCallTo3Bet(String name,BaseHand bh,String way1) {
		Boolean flag=false;
		
		List<WayAction> actions=null;
		actions=StatsUtil.filterBetsGTZ(null,bh,new String[]{Action.RAISE.toString()},new String[]{way1});
		if(actions.size()>1 && !actions.get(1).player.name.equals(name)){
			WayAction sr=null;
			WayAction f=actions.get(1);
			if(actions.size()>2){
				sr=actions.get(2);
			}
			int kk=index(f,bh);
			actions=StatsUtil.filterBetsGTZ(name,bh,new String[]{Action.CALL.toString()},new String[]{way1});
			for(int i=0;i<actions.size() && !flag;i++){
				int x=-1;
				if((x=index(actions.get(i),bh))>kk){
					flag=true;
					if(sr!=null){
						flag=flag && x<index(sr,bh); 
					}
				}
			}
			
		}
		return flag;
	}
	
	public static Boolean isCallTo3BetCount(String name,BaseHand bh, String way) {
		Boolean flag=false;
		List<WayAction> actions=null;
		actions=StatsUtil.filterBetsGTZ(null,bh,new String[]{Action.RAISE.toString()},new String[]{way});
		if(actions.size()>1 && !actions.get(1).player.name.equals(name)){
			actions=StatsUtil.filterBetsGTZ(name,bh,new String[]{Action.CALL.toString(), Action.RAISE.toString()},new String[]{way});
			if(actions.size()>0){
				flag=true;
			}
		}
		return flag;
	}
	
}
